package com.web.cms.pan.dao;

import com.blue.core.dict.OperateType;
import com.web.core.dict.PanExternalType;
import org.springframework.stereotype.Repository;
import com.blue.jdbc.BaseDao;
import com.web.cms.pan.model.OperatorLog;

import java.util.Date;

/**
 * 网盘操作日志数据库访问
 * 
 * @author 代码由 blue-code 框架生成
 * @since 1.0 2018年04月09日
 */
@Repository("panOperatorLogDao")
public class OperatorLogDao extends BaseDao<OperatorLog>
{
	public OperatorLogDao()
	{
	}
	
	@Override
	protected void query(StringBuilder sql, OperatorLog param)
	{
		if (param != null)
		{
			if (param.getUserId() != null && param.getUserId() > 0)
				sql.append(" and a.user_id=:userId");
				
			if (param.getExternalId() != null && param.getExternalId() > 0)
				sql.append(" and a.external_id=:externalId");
				
			if (param.getExternalType() != null)
				sql.append(" and a.external_type=:externalType");

			if (param.getOperateType() != null)
				sql.append(" and a.operate_type=:operateType");
				
		}
		sql.append(" order by a.id desc");
	}

	public OperatorLog create(Integer userId, String descp, Date createTime, Integer externalId, PanExternalType type, OperateType opType)
	{
		OperatorLog log = new OperatorLog();
		log.setUserId(userId);
		log.setDescp(descp.length() > 200 ? descp.substring(0, 200) : descp);
		log.setCreateTime(createTime);
		log.setExternalId(externalId);
		log.setExternalType(type);
		log.setOperateType(opType);
		return log;
	}

	public void add(Integer userId, String descp, Integer externalId, PanExternalType type, OperateType opType)
	{
		OperatorLog log = this.create(userId, descp, new Date(), externalId, type, opType);
		this.save(log);
	}

}
